﻿/*
* Activity Tracking Log v0.1.1 - http://activitytrackinglog.codeplex.com
* Activity Tracking Log for ASP.Net & ASP.Net MVC
* Copyright (c) 2011 Vadim Burac
* Licensed under the Microsoft Public License (Ms-PL)
* http://activitytrackinglog.codeplex.com/license
*/

using System;
using System.Web;
using ActivityTrackingLog.Utils;
using ActivityTrackingLog.Data;
using System.Web.Script.Serialization;
using ActivityTrackingLog.Data.DTOs;

namespace ActivityTrackingLog.Web.Handlers
{
    internal class ClientActivitiesHandler : BaseHandler, IHttpHandler
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            // Return false in case your Managed Handler cannot be reused for another request.
            // Usually this would be false in case you have some state information preserved per request.
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            string securityKey = context.Request["security_key"];
            ActivityItemDTO activityItem = null;
            if (context.Request.RequestType == "POST" && ValidateSecurityKey(securityKey))
            {
                // Log user activity by calling handler from client side scripting
                string category = context.Request["category"];
                string activity = context.Request["activity"];
                string activityCode = context.Request["activity_code"];
                string pageUrl = context.Request["page_url"];
                string queryString = context.Request["query_string"];

                // validate parameters
                if (!string.IsNullOrWhiteSpace(activity))
                {
                    activityItem = Log.LogActivity(ActivityTypeEnum.UserClientSide, activity, false, category, false, activityCode, pageUrl, queryString);
                }
            }
            context.Response.ContentType = "application/json";
            context.Response.ContentEncoding = System.Text.UTF8Encoding.Default;
            if (null != activityItem)
            {
                JavaScriptSerializer js = new JavaScriptSerializer();
                context.Response.Write(js.Serialize(activityItem));
            }
            else
            {
                context.Response.Write("{ 'result': false }");
            }
            context.Response.End();
        }

        #endregion

        #region helpers

        #endregion
    }
}
